<template>
    <div id="app">
        <router-view @refresh-token="refreshToken"></router-view>
    </div>
</template>

<script>
const fs = require("fs");
import { ipcRenderer } from 'electron';
const path = require("path");
const dataPath = require("electron").remote.app.getPath('userData');
const fileUrl = path.join(dataPath, 'config.json');

let config;
if (fs.existsSync(fileUrl)) {
  config = JSON.parse(fs.readFileSync(fileUrl));
} else {
  config = {};
  fs.writeFileSync(fileUrl, '{}');
}
export default {
  name: 'HeCaiYun',
  created() {
    if (config.Token && config.Token.refresh_token) {
      let { expires_in, access_token, refresh_token } = config.Token;
      if (config.Token.expires_in > 3600 * 24) {
        // config.Token.Access_Token
        this.$store.commit('SAVE_ACCESSTOEKN', { expires_in, access_token, refresh_token });
        this.$router.push('admin');
      }
    }
  },
  methods: {
    refreshToken() {
      ipcRenderer.send('refresh-token', {
        id: this.$store.state.Config.APPID,
        key: this.$store.state.Config.APPKEY,
        refresh_token: config.Token.refresh_token
      });
      ipcRenderer.once('token-back', (event, arg) => {
        if (arg.access_token) {
          this.$store.commit('SAVE_ACCESSTOEKN', {
            access_token: arg.access_token,
            expires_in: (Date.now() / 1000) + 2592000,
            refresh_token: config.Token.refresh_token
          });
          config.Token.access_token = arg.access_token;
          fs.writeFileSync(fileUrl, JSON.stringify(config, null, 4));
          new window.Notification('通知', {
            title: '通知',
            body: '重新授权成功，请重新刷新一下页面。'
          });
        } else {
          console.log(arg, '刷新接口失败！');
        }
      });
    }
  }
}
</script>

<style>
html {
  overflow-x: hidden;
  user-select: none;
}
::-webkit-scrollbar {
  width: 19px;
  height: 10px;
  overflow: visible;
}
::-webkit-scrollbar-button {
  width: 0;
  height: 0;
}
::-webkit-scrollbar-thumb {
  background-color: rgb(216, 220, 229);
  border: solid transparent;
  border-width: 1px 0 1px 8px;
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover {
  background-color: rgb(145, 152, 165);
}

.mod-selectable-helper {
    position: fixed;
    z-index: 888;
    border: 1px solid #DBDBDB;
    background-color: rgba(85,85,85,.15);
}
</style>
